<?
 session_start();
 require_once("core.php");

 get_all_variables();
 $db = dbconnect();
 
 if (@$_SESSION['client']==0) { header('Location: /login.php');};

 /* Check if empty */
 if ((@$problem=="")||(@$lang=="")) header('Location: /submit.php');

 $q = "
  INSERT INTO `submits` ( `problem_id` , `client_id` , `time` , `lang` , `active` , `test` , `result` )
  VALUES (
  '$problem', '$client', NOW(), '$lang', 'active', '', ''
  );
   ";
 $r = mysql_query($q, $db);

 $q = "SELECT MAX( id )
  FROM `submits`
  WHERE client_id =".$client;
 $r = mysql_query($q, $db) or die("Server Error!");
 $w = mysql_fetch_array($r);
 $id = $w[0];

 $old = umask(0);
 mkdir("ooengine/lib/submits/".(string)$id, 0777);
 umask($old);
 
 if ($lang!="java")
 {
  @move_uploaded_file($_FILES['solution']['tmp_name'], "ooengine/lib/submits/".(string)$id."/program.".$lang);
  @chmod("ooengine/lib/submits/".(string)$id.'/program.'.$lang,0777);
 }
 else {
  @move_uploaded_file($_FILES['solution']['tmp_name'], "ooengine/lib/submits/".(string)$id."/Main.java");
  @chmod("ooengine/lib/submits/".(string)$id.'/Main.java',0777);
 }; 
 

 $q = "SELECT *
  FROM `submits`
  WHERE id =".$id;
 $r = mysql_query($q, $db) or die("Server Error!");
 $w = mysql_fetch_array($r);

 chdir('ooengine/lib');
 ?> <!-- <?
 system("ruby phprun.rb $id ".$w["problem_id"]." ".$w["lang"]." <secret.txt");
 ?> ---> <?
 chdir('../../');

 if (file_exists('ooengine/lib/submits/'.(string)$id.'/report.xml')) { 
    $xml = simplexml_load_file('ooengine/lib/submits/'.(string)$id.'/report.xml');

    $tests = $xml['tests'];
    $result = $xml;
 } else
 {
    $tests = 0;
    $result = "failed";
 }

$q = "UPDATE `submits` SET `test` = '$tests',
`result` = '$result' WHERE `id` =$id LIMIT 1 ;";

$r = mysql_query($q, $db) or die("Server Error!");

//$filename = 'rscreen.php';

if ((int)$tests>3) {
     $client_result = "Accepted";
}
else {
     $client_result = $result;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>openOli | Judge</title>
<link rel="icon" href="img/favicon.png" type="image/png" />
<link href="css/yahoo_reset.css" rel="stylesheet" type="text/css" />
<link href="facebox/facebox.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/submit.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="facebox/facebox.js"></script>
<script type="text/javascript">
  $(document).ready(function(){
        $(".pInfo").mouseover(function() { this.id="pInfoOn"})
        $(".pInfo").mouseout(function() { this.id=""})
        $('a[rel*=facebox]').facebox();
        setInterval ( "updateSide()", 2000 );
        setInterval(function(){$("#timebox").load("time.php");}, 30000);
        })
</script>
</head>

<body>
  <!-- openOli::Contest | Turan Rustamli -->
  <div id="page">
  
    <div id="hdtext">
	   <div id="hdt_l">open source web based judge for holding olympiads in informatics</div>
	   <div id="hdt_r">English &nbsp; <img src="img/ico_mail.gif" alt="openOli" border="0"> &nbsp; <img src="img/ico_home.gif" alt="openOli" border="0"></div>
	</div>
    
	<div id="header">
	   <div id="logo"><a href="dashboard.php"><img src="img/logo.jpg" alt="openOli" border="0"></a></div>
	   <div id="topmenu">
	        <a class="foxbtn" href="dashboard.php"><span>Dashboard</span></a>
	        <a class="foxbtn" href="problems.php"><span>Problems</span></a>
            <a class="foxbtn_x" href="submit.php"><span>Submit</span></a>
            <a class="foxbtn" href="test.php"><span>Test</span></a>
            <a class="foxbtn" href="archive.php"><span>History</span></a>
            <a class="foxbtn" href="help.php"><span>Help</span></a>
            <a class="foxbtn" href="logout.php"><span>Log out</span></a>
	   </div> 	    
	   <div id="toolbox">		  
		  <div id="timebox">
	          <? include('time.php'); ?>
		  </div>
		</div> 	    
	</div>
	
	<div id="content">
	   <table width="100%" border="0" cellspacing="2">
		<tr>
			<td>
			   <p class="p_title">Result</p>
               <div style="margin-left: 10px;">
                  The result is provided only for first 3 tests
			   </div>  
			   <br/>

               <div class="subsection">
                 <h1 style="font-size: 42px; <? if ($client_result=="Accepted") echo "color:#007F0E;" ?>"><? echo $client_result; ?></h1>

               </div>
                
               </div>
               <br/>
               	
			</td>
			<td id="sidebar">
			   <div id="msgs"><? include("messages.php") ?></div>
			</td>
		</tr>
       </table>
	</div>	
	
	<div id="bottom"><span id="b_left">&copy; Turan Rustamli - You may use openOli under the terms of the MIT License.</div></div>	
	<br />
  </div>
</body>
</html>
